package com.example.bloodbankapplication.fragment;

import android.app.AlertDialog;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;

import com.example.bloodbankapplication.LoginActivity;

public class BaseFragment extends Fragment {
	private ProgressDialog dialog;
	protected final String PREFERANCE ="BloodBank";
	protected SharedPreferences mPreferance;
	protected InputMethodManager inputMethodManager;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		System.out.println("Chandra");
		return super.onCreateView(inflater, container, savedInstanceState);
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		inputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		mPreferance = getActivity().getSharedPreferences(PREFERANCE, 0);
		
	       
	}
	
	protected void hideSoftKeyPad(){
		if(inputMethodManager.isActive()){
		 inputMethodManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);}
	}

	protected void startProgressDialog() {

		dialog = new ProgressDialog(getActivity());
		dialog.setTitle("BloodBank");
		dialog.setMessage("Loading...");
		dialog.show();

	}

	protected void stopProgressDialog() {

		dialog.dismiss();
	}
	
	protected  void logout(){
		
		Intent intent = new Intent(getActivity(),LoginActivity.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(intent);
	}
	
	protected boolean getConnectivity(){
		ConnectivityManager cm = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
		if(cm.getActiveNetworkInfo()!=null){
			return cm.getActiveNetworkInfo().isConnectedOrConnecting();
		}
		return false;
	}
	
	protected void showPopUp(Context context,String message){
		
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle("BloodBank");
		builder.setMessage(message);
		builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				
			}
		});
		
		builder.show();
	}

}
